﻿Imports OpenFlashChart
Imports System.Data.OleDb

Partial Public Class getPieChartData
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.CacheControl = "no-cache"
        Dim Chart As New Graph
        Dim CD As OpenFlashChart.Charts.Pie


        CD = New OpenFlashChart.Charts.Pie(75, "#ff0000", "#0000FF")



        Dim tagCountSql As String = "SELECT papersweeksandtags.Tag, Count(*) AS TagCount FROM papersweeksandtags GROUP BY papersweeksandtags.Tag"
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("mobilestandardsdb").ConnectionString)
        oleDbConn.Open()
        Dim tagCountCmd As New OleDbCommand(tagCountSql, oleDbConn)
        Dim tagCountReader = tagCountCmd.ExecuteReader()
        Dim tagHash As New Hashtable
        Dim maxTagCt As Integer = 0
        Dim minTagCt As Integer = 0
        Dim count As Integer = 0
        Dim numRows As Integer = 7
        If tagCountReader.HasRows() Then
            While tagCountReader.Read()
                count = count + 1
                Dim thisGrayshade = Math.Truncate(255 / numRows * count)
                Dim thiscolor As String = RGB2HTMLColor(thisGrayshade, thisGrayshade, thisGrayshade)
                CD.Data.Add(New OpenFlashChart.Charts.Pie.Piece(tagCountReader("TagCount"), tagCountReader("Tag"), thiscolor))
                If minTagCt = 0 Then minTagCt = CInt(tagCountReader("TagCount"))
                If minTagCt > CInt(tagCountReader("TagCount")) Then minTagCt = CInt(tagCountReader("TagCount"))
                If maxTagCt < CInt(tagCountReader("TagCount")) Then maxTagCt = CInt(tagCountReader("TagCount"))
            End While
        End If
        Chart.MaxY = maxTagCt

        Chart.Pie = CD


        Response.Write(Chart.ToString)


    End Sub

    Public Function RGB2HTMLColor(ByVal R As Byte, ByVal G As Byte, _
   ByVal B As Byte) As String


        'INPUT: Numeric (Base 10) Values for R, G, and B)

        'RETURNS:
        'A string that can be used as an HTML Color
        '(i.e., "#" + the Hexadecimal equivalent)

        Dim HexR, HexB, HexG As Object
        Dim sTemp As String

        On Error GoTo ErrorHandler

        'R
        HexR = Hex(R)
        If Len(HexR) < 2 Then HexR = "0" & HexR

        'Get Green Hex
        HexG = Hex(G)
        If Len(HexG) < 2 Then HexG = "0" & HexG

        HexB = Hex(B)
        If Len(HexB) < 2 Then HexB = "0" & HexB



        RGB2HTMLColor = "#" & HexR & HexG & HexB
ErrorHandler:
    End Function

End Class